
var app = getApp()
var fun = require('../fun.js');

Page({
  data: {
    nav:{top:app.globalData.nav_top,title:'订单支付',home:'show',back:'show',safe_top:app.globalData.safe_top,bottom:app.globalData.safe_bottom,page:'cart'},page:1,can_load:1,share_bt:'1',can_buy:0
  },
  onLoad: function (op) {
var id=op.id;if(!id){app.err('请指定订单');return;}
this.data.id=id;
  },
  onReady: function () {
  fun.get_pos().then(res=>{//活动地址后进行
      if(res.err=='ok'){//用户的经纬度
        app.globalData.latitude=res.latitude; 
        app.globalData.longitude=res.longitude;
      }
      this.load();
        })
  },

  onShow: function () {
    if(app.globalData.is_select_position){//选点
var loc=app.globalData.is_select_position;
console.log(loc)
    var data={};
    var list=this.data.list;
    data.latitude=loc.latitude;
    data.longitude=loc.longitude;
    data.addr=loc.address;
    data.pro=loc.pro;
    data.city=loc.city;
    data.dist=loc.dist;
    console.log('用户数据',data)
//距离
var shop=this.data.shop;
if(shop.latitude){
var dis=fun.get_dis(shop.latitude,shop.longitude,data.latitude,data.longitude);
console.log('距离',dis)
var info=this.data.info;if(!info){info={}}
info.dis=dis;
list.dis=dis;
this.setData({info:info})
console.log(dis)
}
   this.setData({user_data:data,user_change:1});
   this.user_save(1);
    }
    app.globalData.is_select_position='';
if(app.globalData.flash==1){this.load()}
app.globalData.flash=0

  },
  select_yun(e){
var val=e.currentTarget.dataset.val;
var list=this.data.list;
this.setData({yun_show:''})
fun.get({ac:'order_set_yun',id:list.id,val:val}).then(res=>{
this.load();
})


  },
  pay: function () {fun.view_add('支付');
  var that=this;//支付
  var is_pay=this.data.is_pay;
  if(is_pay==1){return;}
  var list=this.data.list;
  that.setData({is_pay:1});
  setTimeout(function(){that.setData({is_pay:''})},15000);//5秒钟后恢复
  fun.get({ac:'pay',id:list.id,path:'hd'}).then(res=>{
 if(res.err=='no_login'){return;}
 wx.requestPayment({
    timeStamp: res.pay.timeStamp,
    nonceStr: res.pay.nonceStr,
    package: res.pay.package,
    signType: 'MD5',
    paySign: res.pay.paySign,
    success(res) {
    if (res.errMsg == 'requestPayment:ok') {
      list.state=1;
      that.setData({pay_suc_show:'show',list:list});//打开支付成功页面
//清除订单
app.globalData.clean_cart=1;
    }
    },
    complete(res){//清除订单的缓存
      that.setData({is_pay:''});
fun.get({ac:'clean_order',path:'hd',id:list.id}).then(res=>{
  
})
    }
    }
    );
 })
},
  onHide: function () {

  },


  load: function () {
var id=this.data.id;
fun.get({ac:'get_cart',path:'hd',id:id}).then(res=>{
  var shop;
if(res.shop){shop=res.shop;
if(app.globalData.latitude && shop.latitude){
  var dis=fun.get_dis(app.globalData.latitude,app.globalData.longitude,shop.latitude,shop.longitude);
  console.log('门店距离',dis);
  shop.dis=dis;
}
}

this.setData({
app:res.app,
can_pay:res.can_pay,
hd:res.hd,
pro:res.pro,
miao:res.miao,
mz:res.mz,
shop:shop,
user:res.user,
yun:res.yun,
list:res.list,
quan:res.quan,
msg_inout:res.list.msg?res.list.msg:''
})


})


  },
  get_addr(){//获取微信联系人地址簿
    var that=this;
    wx.chooseAddress({
      success (res) {
    console.log(res)
    var data={}
    data.name=res.userName;
    data.phone=res.telNumber;
    data.pro=res.provinceName;
    data.city=res.cityName;
    data.dist=res.countyName;
    data.addr=res.detailInfo;
    that.setData({user_data:data,user_change:1})
    that.user_save(1)
      }
    })

  },user_save(ac){//修改用户和订单信息
    var data=this.data.user_data;
    var user_change=this.data.user_change;if(user_change!=1){return;}
    if(!data){return;}
    var list=this.data.list;
      fun.get({ac:'order_user_change',data:JSON.stringify(data),id:list.id}).then(res=>{
        if(res.err=='no_login'){return;}
    for(var i in data){
      list[i]=data[i];
    }
    this.setData({list:list,user_data:'',user_change:0});
    if(ac==1){this.load()}
      })
      }, 
      region_select: function (e) {//选择地区
        var val=e.detail.value;
        console.log(val)
        var user_data={pro:val[0],city:val[1],dist:val[2]}
        var list=this.data.list;
        list['region']=val;
        this.setData({user_data:user_data,user_change:1,list:list});
        if(list.dist!=user_data.dist){this.user_save(1);}else{this.user_save();}//如果调整超过区级,则重新加载地址
          },
      user_change: function (e) {//修改数据
        var c=e.currentTarget.dataset.c;
        var val=e.detail.value;
        console.log(c,val);
        this.setData({user_data:{[c]:val},user_change:1})
          },
          quan_user_it(e){
var list=this.data.list;
var quan=this.data.quan;
var index=e.currentTarget.dataset.index;
var q=quan[index];
var man=parseFloat(q.man);
var price=parseFloat(list.price);
if(price>=man){
fun.get({ac:'set_quan',path:'hd',id:list.id,quan_id:q.id}).then(res=>{
  app.msg('优惠券选择成功');
  this.load();
})

}else{app.msg('抱歉，您的订单金额不满'+man+'元，您可以返回继续下单。')}


          },
          msg_input(e){//输入信息
            var val=e.detail.value;
            this.data.msg_text=val;
          },
          submit_msg(e){//提交订单留言
          var val=this.data.msg_text;
          if(!val){app.msg('请输入订单留言');return;}
          var list=this.data.list;
          list.msg=val;
          this.setData({list:list,msg_input_show:''});
          //为了调整性能 先关闭 后台进行数据更新
          fun.get({ac:'order_msg_submit',id:list.id,val:val,path:'hd'}).then(res=>{
           // this.setData({list:list,msg_input_show:''})
          })

          },

  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },go(e){ 
    var url=e.currentTarget.dataset.url;
    if(!url){app.msg('没有设置链接，请到后台设置或者联系我们');return;}
    wx.navigateTo({url: e.currentTarget.dataset.url})},
  back(e){wx.navigateBack({delta:0})},
  home(e){ wx.reLaunch({url: '/pages/index/index'})},
  rgo(e){wx.redirectTo({url: e.currentTarget.dataset.url})},
  login: function (e) { wx.navigateTo({url: '/pages/login/index'})},
  win_open(e){var c=e.currentTarget.dataset.c;this.setData({[c]:'show'})},//打开窗口
  win_close(e){var c=e.currentTarget.dataset.c;this.setData({[c]:''})},//关闭窗口
open_min(e){wx.navigateToMiniProgram({appId: e.currentTarget.dataset.url,path: '/pages/index/index',})},
scan(e){fun.scan();},
daohang(e){
   var shop=this.data.shop
   wx.navigateTo({
     url: '/pages/map/index?ac=daohang&lat='+shop.latitude+'&lng='+shop.longitude,
   })

},
  call(e){
var shop=this.data.shop
wx.makePhoneCall({
  phoneNumber: shop.phone,
})

  },get_position: function () {fun.view_add('打开地图选点');
var list=this.data.list;
if(!list.latitude){
   if(app.globalData.latitude){//保存订单参数
      var data={latitude:app.globalData.latitude,longitude:app.globalData.longitude}
      fun.get({ac:'order_user_change',id:list.id,data:JSON.stringify(data)}).then(res=>{})//保存
list.latitude=app.globalData.latitude;
list.longitude=app.globalData.longitude;
   }
}
wx.navigateTo({
  url: '/pages/map/index?latitude='+list.latitude+'&longitude='+list.longitude+'&ac=point',
})
  }
})
